package projeto.model.testes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import projeto.model.*;
/**
 * 
 * @author Javatar
 *
 */
public class TestaUsuario {
	private Usuario userConstrutor;
	private List<Integer> opnioes1,opnioes2,opnioes3,opnioesNula,opnioesTamanhoInvalido;
	private Usuario user1,user2,user3,userIgual;
	private Integer[] opinioes1 = {4,2,0,0,5,1,1,1,-1,2,-4,2,0,-5,-2,4,0,0,3,3,-2,0,0,0,4,3,0,0,0,0,-1,0,0,0,2,0,0,0,2};
	private List<Integer> listaOpinioes1 = Arrays.asList(opinioes1);
	private Integer[] opinioes2 = {3,0,0,0,0,0,0,0,2,3,2,0,0,1,0,4,0,0,0,5,0,0,0,0,0,0,0,0,1,2,0,0,0,2,0,0,3,0,0};
	private List<Integer> listaOpinioes2 = Arrays.asList(opinioes2);
	private Integer[] opinioes3 = {3,0,0,0,0,5,5,3,0,5,0,0,0,0,5,0,0,-4,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,5,0,0};
	private List<Integer> listaOpinioes3 = Arrays.asList(opinioes3);
	
	@Before
	public void criaUsuario() throws Exception{
		user1 = new Usuario("13/05/2011 18:35:48","Nazareno",listaOpinioes1);
		user2 = new Usuario("13/05/2011 18:46:59","Anonimo75",listaOpinioes2);
		user3 = new Usuario("13/05/2011 18:47:23","Anonimo76",listaOpinioes3);
		userIgual = new Usuario("13/05/2011 18:47:23","Anonimo76",listaOpinioes3);
		opnioes1 = user1.getOpnioes();
		opnioes2 = user2.getOpnioes();
		opnioes3 = user3.getOpnioes();
		opnioesNula = null;
	}

	@Test
	public void testUsuario() {
		
		try {
			userConstrutor = new Usuario("data","nome",opnioes1);
		} catch (Exception e) {
			Assert.fail(e.getMessage());
		}
		
		try {
			userConstrutor = new Usuario("","nome",opnioes1);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Data vazia.",e.getMessage());
		}
		
		try {
			userConstrutor = new Usuario(null,"nome",opnioes1);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Data Nula.",e.getMessage());
		}
		
		try {
			userConstrutor = new Usuario("data",null,opnioes1);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Nome Nulo.",e.getMessage());
		}
		
		try {
			userConstrutor = new Usuario("data","nome",opnioesNula);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Opnioes Nulas",e.getMessage());
		}
	}

	@Test
	public void testGetNome() {
		Assert.assertEquals("Nazareno", user1.getNome());
		Assert.assertEquals("Anonimo75", user2.getNome());
		Assert.assertEquals("Anonimo76", user3.getNome());
	}

	@Test
	public void testGetData() {
		Assert.assertEquals("13/05/2011 18:35:48", user1.getData());
		Assert.assertEquals("13/05/2011 18:46:59", user2.getData());
		Assert.assertEquals("13/05/2011 18:47:23", user3.getData());
	}

	@Test
	public void testGetOpnioes() {
		//teste de opnioes detalhadas por toString()
		Assert.assertEquals("[4, 2, 0, 0, 5, 1, 1, 1, -1, 2, -4, 2, 0, -5, -2, 4, 0, 0, 3, 3, -2, 0, 0, 0, 4, 3, 0, 0, 0, 0, -1, 0, 0, 0, 2, 0, 0, 0, 2]", user1.getOpnioes().toString());
		//Outros testes de opnioes menos detalhados
		Assert.assertEquals(opnioes2, user2.getOpnioes());
		Assert.assertEquals(opnioes3, user3.getOpnioes());
		
		Assert.assertEquals(39, user1.getOpnioes().size());
	}

	@Test
	public void testSetNome() throws Exception {
		user1.setNome("Nazareno2");
		Assert.assertEquals("Nazareno2", user1.getNome());
		
		try {
			user1.setNome(null);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Nome Nulo.",e.getMessage());
		}
		
		try {
			user1.setNome("");
			Assert.assertEquals("Anonimo 1", user1.getNome());
		} catch (Exception e) {
			Assert.fail(e.getMessage());
		}
		
		try {
			user1.setNome("Joao");
		} catch (Exception e) {
			Assert.fail(e.getMessage());
		}
		user1.setNome("Nazareno");//para nao ficar alterado
	}

	@Test
	public void testSetOpinioes() throws Exception {
		try {
			user1.setOpinioes(opnioesNula);
			Assert.fail();
		} catch (Exception e) {
			Assert.assertEquals("Opnioes Nulas",e.getMessage());
		}
		
		//um teste valido
		user1.setOpinioes(opnioes2);
		Assert.assertEquals(opnioes2, user1.getOpnioes());
		user1.setOpinioes(opnioes1);//para nao ficar alterado
	}

	@Test
	public void testToString() {
		Assert.assertEquals("13/05/2011 18:35:48;Nazareno;4;2;0;0;5;1;1;1;-1;2;-4;2;0;-5;-2;4;0;0;3;3;-2;0;0;0;4;3;0;0;0;0;-1;0;0;0;2;0;0;0;2", user1.toString());
		
		Assert.assertEquals("13/05/2011 18:46:59;Anonimo75;3;0;0;0;0;0;0;0;2;3;2;0;0;1;0;4;0;0;0;5;0;0;0;0;0;0;0;0;1;2;0;0;0;2;0;0;3;0;0", user2.toString());
		
		Assert.assertEquals("13/05/2011 18:47:23;Anonimo76;3;0;0;0;0;5;5;3;0;5;0;0;0;0;5;0;0;-4;0;0;0;0;0;0;0;5;0;0;0;0;0;0;0;0;0;0;5;0;0", user3.toString());
		
	}
	
	@Test
	public void testEquals(){
		Assert.assertFalse(user1.equals(user2));
		Assert.assertFalse(user2.equals(user3));
		Assert.assertFalse(user1.equals(user3));
		Assert.assertTrue(user3.equals(userIgual));
	}
	
	@Test
	public void testHashCode(){
		Assert.assertFalse(user1.hashCode() == user2.hashCode());
		Assert.assertFalse(user2.hashCode() == user3.hashCode());
		Assert.assertFalse(user1.hashCode() == user3.hashCode());
		Assert.assertTrue(user3.hashCode() == userIgual.hashCode());
	}

}